# Maintainer: Felix Yan <felixonmars@archlinux.org>

_hkgname=persistent-postgresql
pkgname=haskell-persistent-postgresql
pkgver=2.13.4.0
pkgrel=82
pkgdesc="Backend for the persistent library using postgresql."
url="http://www.yesodweb.com/book/persistent"
license=("MIT")
arch=('loong64' 'x86_64')
depends=('ghc-libs' 'haskell-aeson' 'haskell-attoparsec' 'haskell-blaze-builder' 'haskell-conduit'
         'haskell-monad-logger' 'haskell-persistent' 'haskell-postgresql-libpq'
         'haskell-postgresql-simple' 'haskell-resource-pool' 'haskell-resourcet'
         'haskell-string-conversions' 'haskell-unliftio-core' 'haskell-vault')
makedepends=('ghc' 'haskell-hunit' 'haskell-quickcheck' 'haskell-fast-logger' 'haskell-hspec'
             'haskell-hspec-expectations' 'haskell-hspec-expectations-lifted'
             'haskell-http-api-data' 'haskell-path-pieces' 'haskell-persistent-qq'
             'haskell-persistent-test' 'haskell-quickcheck-instances' 'haskell-unliftio'
             'haskell-unordered-containers' 'haskell-vector')
checkdepends=('pifpaf' 'postgresql')
source=("https://hackage.haskell.org/packages/archive/$_hkgname/$pkgver/$_hkgname-$pkgver.tar.gz")
sha512sums=('0c250736a28a8c12ba1182758eaf764c704d025bcb34cbd8d9cdea3639606bf9c8433400de3f1441f590002797babde59ac6966c6cde49b41a976891cd47c9fd')

prepare() {
  cd $_hkgname-$pkgver
  # pifpaf's default port
  sed -i 's/port=5432/port=9824/' test/PgInit.hs
}

build() {
  cd $_hkgname-$pkgver

  runhaskell Setup configure -O --enable-shared --enable-executable-dynamic --disable-library-vanilla \
    --prefix=/usr --docdir=/usr/share/doc/$pkgname --datasubdir=$pkgname --enable-tests \
    --dynlibdir=/usr/lib --libsubdir=\$compiler/site-local/\$pkgid --ghc-option=-fllvm \
    --ghc-option=-optl-Wl\,-z\,relro\,-z\,now \
    --ghc-option='-pie'

  runhaskell Setup build $MAKEFLAGS
  runhaskell Setup register --gen-script
  runhaskell Setup unregister --gen-script
  sed -i -r -e "s|ghc-pkg.*update[^ ]* |&'--force' |" register.sh
  sed -i -r -e "s|ghc-pkg.*unregister[^ ]* |&'--force' |" unregister.sh
}

check() {
  cd $_hkgname-$pkgver
  eval $(pifpaf run postgresql --host 127.0.0.1)
  createuser -s postgres
  createdb test
  # https://github.com/yesodweb/persistent/issues/1278
  runhaskell Setup test --show-details=direct || echo "Tests failed"
  pifpaf_stop
}

package() {
  cd $_hkgname-$pkgver

  install -D -m744 register.sh "$pkgdir"/usr/share/haskell/register/$pkgname.sh
  install -D -m744 unregister.sh "$pkgdir"/usr/share/haskell/unregister/$pkgname.sh
  runhaskell Setup copy --destdir="$pkgdir"
  install -D -m644 LICENSE -t "$pkgdir"/usr/share/licenses/$pkgname/
  rm -f "$pkgdir"/usr/share/doc/$pkgname/LICENSE
}
